# ==================== 编译配置 ====================
CC = gcc
CFLAGS = -Wall -Wextra -std=c99 -O2 -g -D_GNU_SOURCE
INCLUDES = -Iinclude
LIBS = -lpthread

# 目录配置
SRCDIR = src
INCDIR = include
OBJDIR = obj
BINDIR = bin

# 源文件
SOURCES = $(wildcard $(SRCDIR)/*.c)
OBJECTS = $(SOURCES:$(SRCDIR)/%.c=$(OBJDIR)/%.o)

# 目标文件
TARGET = $(BINDIR)/light_redis

# ==================== 默认目标 ====================
all: $(TARGET)

# ==================== 主程序 ====================
$(TARGET): $(OBJDIR)/main.o $(OBJDIR)/cache.o $(OBJDIR)/eviction.o $(OBJDIR)/command.o $(OBJDIR)/network.o $(OBJDIR)/threadpool.o $(OBJDIR)/storage.o
	@mkdir -p $(BINDIR)
	$(CC) $^ -o $@ $(LIBS)
	@echo "编译完成: $@"

# ==================== 目标文件规则 ====================
$(OBJDIR)/%.o: $(SRCDIR)/%.c
	@mkdir -p $(OBJDIR)
	$(CC) $(CFLAGS) $(INCLUDES) -c $< -o $@

# ==================== 清理 ====================
clean:
	rm -rf $(OBJDIR) $(BINDIR)
	@echo "清理完成"

# ==================== 运行 ====================
run: $(TARGET)
	@echo "启动 Light Redis 服务器..."
	./$(TARGET)

# ==================== 测试 ====================
test: $(TARGET)
	@echo "运行基础测试..."
	./$(TARGET)

# ==================== 帮助信息 ====================
help:
	@echo "可用的目标:"
	@echo "  all        - 编译项目"
	@echo "  clean      - 清理编译文件"
	@echo "  run        - 编译并运行服务器"
	@echo "  test       - 运行基础测试"
	@echo "  help       - 显示此帮助信息"

# ==================== 伪目标 ====================
.PHONY: all clean run test help